package chapter3.No3_1_等待和通知机制.j生产者_消费者模式实现.demo3;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/19.
 */
public class MyStack {
    private List list = new ArrayList();

    synchronized public void push() {
        try {
            if (list.size() != 0) {
                this.wait();
            }
            double random = Math.random();
            String value = "anyString" + random;
            list.add(value);
            System.out.println(value);
            System.out.println("list.size()=" + list.size());
            this.notify();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public void pop() {
        try {
            if (list.size() == 0) {
                this.wait();
            }
            System.out.println(list.get(0));
            list.remove(0);
            System.out.println("list.size()=" + list.size());
            this.notify();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
